package com.htbb.model.personalInsure;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.util.StringUtils;

import com.htbb.common.base.MappingConfig;
import com.htbb.common.base.exception.BizException;
import com.htbb.common.util.AidCreator;
import com.htbb.common.util.CheckIdCardUtils;
import com.htbb.common.util.UtilDate;
import com.htbb.model.personalInsure.dto.Channel;
import com.htbb.model.personalInsure.entity.CheckInfo;
import com.htbb.order.entity.Product;
import com.htbb.service.personalInsure.IsuBizService;

/**
 * 核保请求信息模型
 * 
 * @author 
 * 		liulongli
 * @date 
 * 		2017-5-19 17:07:43
 */
public class ReqOfIsuChk implements Serializable{

	private static final long serialVersionUID = 1L;
	
	/*合作伙伴代码*/
	private String partnerCode;
	/*交易流水号*/
	private String tradeId;
	
	/*投保人信息*/
	private String custName; // 投保人姓名
	private String custPhone; //投保人身份证号
	private String custShip; // 投保人与被保险人关系	本人（默认），配偶，子女，父母，其他
	private String custEmail; //投保人邮箱
	private String custSsn; //投保人身份证号
	
	/*被保险人信息*/
	private String isuPhone; //被保人身份证号
	private String isuName; // 被保人姓名
	private String isuSsn; // 被保人证件号码
	private Integer isuIdType; //被保人证件类型  1身份证
	private String isuAddress; // 被保人住址
	private String isuEmail; //被保险人邮箱 
	private String birthday; // 被保人出生日期	格式 yyyy-MM-dd
	private String jobType; // 职位类别
	
	/*产品类型、起止保时间*/
	private String prodType; //购买产品类型	见附录-产品类型
	private String isuDtStart; //起保日期
	private String isuDtEnd; // 止保日期
	
	/**
	 * 核验保险下单信息
	 * @return
	 * @throws ParseException 
	 */
	public CheckInfo check(IsuBizService isuBizService, Channel channel) throws Exception{
		
		Map<String, Product> productMap = channel.getProductMap();
		Product product = productMap.get(this.getProdType().toUpperCase());
		if(product == null)
			throw new BizException("产品类型无效，请重新输入");
		String skuAid = product.getSkuid();
		String skuType = product.getSkuType();
		CheckInfo checkInfo = new CheckInfo();
		checkInfo.setPartnerId(channel.getPartnerId());
		checkInfo.setPartnerCode(channel.getPartnerCode());
		checkInfo.setChannelTradeId(channel.getPartnerCode() + "_psn_" + this.getTradeId());
		checkInfo.setSkuType(skuType);
		checkInfo.setFpCusr("htbb-interface");
		checkInfo.setFpMusr("htbb-interface");
		checkInfo.setCustName(this.custName);
		checkInfo.setCustPhone(this.getCustPhone());
		checkInfo.setCustShip(this.getCustShip());
		checkInfo.setCustEmail(this.getCustEmail());
		checkInfo.setCustSsn(this.getCustSsn());
		checkInfo.setIsuPhone(this.getIsuPhone());
		checkInfo.setIsuName(this.getIsuName());
		checkInfo.setIsuSsn(this.getIsuSsn());
		checkInfo.setIsuIdType(this.getIsuIdType());
		checkInfo.setIsuAddress(this.getIsuAddress());
		checkInfo.setIsuEmail(this.getIsuEmail());
		checkInfo.setBirthDay(this.getBirthay());
		checkInfo.setJobType(this.getJobType());
		checkInfo.setIsuDtStart(this.getIsuDtStart());
		checkInfo.setIsuDtEnd(this.getIsuDtEnd());

		if(this.getProdType().indexOf("AJB") != -1){//安居宝
			if(StringUtils.isEmpty(this.getIsuAddress())){
				throw new BizException("被保人地址不能为空");
			}
		}
		
		/**
		 * 验证身份证号是否合法
		 */
		if (!CheckIdCardUtils.validateCard(isuSsn)) {
			throw new BizException("被保人身份证无效");
		}
		if (!CheckIdCardUtils.validateCard(custSsn)) {
			throw new BizException("投保人身份证无效");
		}

		/**
		 * 验证产品信息
		 */
		checkAgeProduct(custSsn, isuSsn, isuDtStart, isuDtEnd, product, checkInfo);
		
		checkInfo.setIsResgister(channel.getIsResgister());
		checkInfo.setMaxInsured(product.getIsumaxage());
		//1代表自动支付渠道
		if(!StringUtils.isEmpty(channel.getIsautopay()) && "1".equals(channel.getIsautopay())){
			checkInfo.setIsuStatus(1);
			checkInfo.setOrdStatus("4");
		}else{
			checkInfo.setIsuStatus(0);
			checkInfo.setOrdStatus("2");
		}
		checkInfo.setAddrUsername(custName);
		
		return checkInfo;
	}
	
	/**
	 * 校验产品信息
	 * 
	 * @param custSsn
	 * @param isuSsn
	 * @param isuDtStart
	 * @param isuDtEnd
	 * @param product
	 * @param checkInfo
	 * @throws ParseException 
	 */
	private void checkAgeProduct(String custSsn, String isuSsn, String isuDtStart, String isuDtEnd, Product product,
			CheckInfo checkInfo) throws ParseException {
		
		float isuAge = (float) CheckIdCardUtils.getAgeByIdCardFloat(isuSsn);
		float custAge = (float) CheckIdCardUtils.getAgeByIdCardFloat(custSsn);

		/**
		 * 验证性别
		 */
		//性别(M-男，F-女，N-未知)
		String gender=CheckIdCardUtils.getGenderByIdCard(isuSsn);
		String sex = product.getGender();
		if(!StringUtils.isEmpty(sex)){
			if(!sex.equals(gender)){
				throw new BizException("被保险人性别有误");
			}
		}
		
		/**
		 * 验证年龄
		 */
		String custMinAge = product.getCustminage();
		String custMaxAge = product.getCustmaxage();
		String isuMinAge = product.getIsuminage();
		String isuMaxAge = product.getIsumaxage();
		if (!StringUtils.isEmpty(custMinAge)) {
			if (custAge < Float.valueOf(custMinAge)) {
				throw new BizException("投保人年龄必须大于等于" + custMinAge);
			}
		}
		if (!StringUtils.isEmpty(custMaxAge) && !"0".equals(custMaxAge)) {
			if (custAge > Float.valueOf(custMaxAge)) {
				throw new BizException("投保人年龄必须小于等于" + custMaxAge);
			}
		}
		if (!StringUtils.isEmpty(isuMinAge)) {
			if (isuAge < Float.valueOf(isuMinAge)) {
				throw new BizException("被保人年龄必须大于等于" + isuMinAge);
			}
		}
		if (!StringUtils.isEmpty(isuMaxAge) && !"0".equals(isuMaxAge)) {
			if (isuAge > Float.valueOf(isuMaxAge)) {
				throw new BizException("被保人年龄必须小于等于" + isuMaxAge);
			}
		}

		/**
		 * 验证起保时间
		 */
		if (!UtilDate.isuStartDateCheck(isuDtStart)) {
			throw new BizException("起保时间T+1日起保");
		}

		/**
		 * entity赋值
		 */
		checkInfo.setProid(product.getPid());
		checkInfo.setSkuid(product.getSkuid());
		String amt = product.getAmt();
		int price = new BigDecimal(amt).multiply(new BigDecimal("100")).intValue();
		checkInfo.setPrice(price);
		checkInfo.setIsuAid(AidCreator.get() + "");
		checkInfo.setOrderAid(AidCreator.get() + "");
		checkInfo.setOrderDetailAid(AidCreator.get() + "");
		checkInfo.setSourceAid(AidCreator.get() + "");

		/**
		 * 验证保期
		 */
		String days = product.getDays();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start;
		try {
			if(!StringUtils.isEmpty(days)){
				start = sf.parse(isuDtStart);
				Date end = sf.parse(isuDtEnd);
				Date correctDate = null;
				if ("365".equals(Integer.valueOf(days))) {
					correctDate = DateUtils.addYears(start, 1);
				} else {
					correctDate = DateUtils.addDays(start, Integer.valueOf(days));
				}

				if (correctDate.getTime() - end.getTime() < 0) {
					throw new BizException("产品类型与保险天数不符");
				}
			}
			
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}
	
	public String getIsuEmail() {
		return isuEmail;
	}
	public void setIsuEmail(String isuEmail) {
		this.isuEmail = isuEmail;
	}
	public String getBirthay() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getJobType() {
		return jobType;
	}
	public void setJobType(String jobType) {
		this.jobType = jobType;
	}
	
	public String getPartnerCode() {
		return partnerCode;
	}
	public void setPartnerCode(String partnerCode) {
		this.partnerCode = partnerCode;
	}
	
	public String getCustName() {
		return custName;
	}
	public void setCustName(String custName) {
		this.custName = custName;
	}
	public String getCustPhone() {
		return custPhone;
	}
	public void setCustPhone(String custPhone) {
		this.custPhone = custPhone;
	}
	public String getCustShip() {
		return custShip;
	}
	public void setCustShip(String custShip) {
		this.custShip = custShip;
	}
	public String getCustEmail() {
		return custEmail;
	}
	public void setCustEmail(String custEmail) {
		this.custEmail = custEmail;
	}
	public String getCustSsn() {
		return custSsn;
	}
	public void setCustSsn(String custSsn) {
		this.custSsn = custSsn;
	}
	public String getIsuName() {
		return isuName;
	}
	public void setIsuName(String isuName) {
		this.isuName = isuName;
	}
	public String getIsuSsn() {
		return isuSsn;
	}
	public void setIsuSsn(String isuSsn) {
		this.isuSsn = isuSsn;
	}
	public String getIsuAddress() {
		return isuAddress;
	}
	public void setIsuAddress(String isuAddress) {
		this.isuAddress = isuAddress;
	}
	public String getIsuDtStart() {
		return isuDtStart;
	}
	public void setIsuDtStart(String isuDtStart) {
		this.isuDtStart = isuDtStart;
	}
	public String getIsuDtEnd() {
		return isuDtEnd;
	}
	public void setIsuDtEnd(String isuDtEnd) {
		this.isuDtEnd = isuDtEnd;
	}
	
	public String getIsuPhone() {
		return isuPhone;
	}
	public void setIsuPhone(String isuPhone) {
		this.isuPhone = isuPhone;
	}
	public String getProdType() {
		return prodType;
	}
	public void setProdType(String prodType) {
		this.prodType = prodType;
	}

	public String getTradeId() {
		return tradeId;
	}

	public void setTradeId(String tradeId) {
		this.tradeId = tradeId;
	}


	public String getBirthday() {
		return birthday;
	}

	public Integer getIsuIdType() {
		return isuIdType;
	}

	public void setIsuIdType(Integer isuIdType) {
		this.isuIdType = isuIdType;
	}
	
}
